//
//  Logger.swift
//  DoShare
//
//  Created by 申铭 on 2024/02/23.
//  Copyright © 2024 eetrust. All rights reserved.
//

import Foundation
import Bangu

let log = Logger.shared

class Logger {
    static let shared = Logger.init()
    private let bangu = Bangu()
    
    private init() {
        #if DEBUG
        let file = FileDestination(logsDirectory: getLogFilePath(subPath: "DebugLogs"))
        file.logFileAmount = 5
        file.encryptLogMessage = false
        let console = ConsoleDestination()
        console.formats = [.level, .thread, .file(type: .onlyFileName), .function, .line, .context]
        bangu.addDestination(console)
        bangu.addDestination(file)
        #else
        let file = FileDestination(logsDirectory: getLogFilePath(subPath: "ReleaseLogs"))
        file.logFileAmount = 5
        file.encryptLogMessage = true
        file.set(encryptKey: "creseassetlogs12", encryptIv: "creseassetlogs12")
        bangu.addDestination(file)
        #endif
        
        verbose("bangu日志文件路径：\(file.logsDirectory?.absoluteString ?? "")")

    }
    
    func verbose(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) {
        bangu.verbose(message(), file, function, line: line, context: context)
    }
    
    func debug(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) {
        bangu.debug(message(), file, function, line: line, context: context)
    }
    
    func info(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) {
        bangu.info(message(), file, function, line: line, context: context)
    }
    
    func warning(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) {
        bangu.warning(message(), file, function, line: line, context: context)
    }
    
    func error(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) {
        bangu.error(message(), file, function, line: line, context: context)
    }
    
    func fault(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) {
        bangu.fault(message(), file, function, line: line, context: context)
    }
    
    func cleanLog() {
        let fileManager = FileManager.default
        for des in bangu.destinations where des is FileDestination {
            if let dest = des as? FileDestination, let path = dest.logsDirectory {
                //删除路径下日志文件夹
                do {
                    try fileManager.removeItem(atPath: path.path)
                } catch {
                    log.error("error while delete files: \(error)")
                }
            }
        }
    }
    
    private func getLogFilePath(subPath: String) -> URL? {
        return URL(string: (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.absoluteString ?? "") + subPath)
    }
}
